﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class VanningBLL
    {
        private static readonly ICartonDA cartonDA = DataAccess.CreateCartonDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IOrderDetailDA orderDetailDA = DataAccess.CreateOrderDetailDA();
        private static readonly IOrdersDA ordersDA = DataAccess.CreateOrdersDA();
        private PubParasInfo pubParasInfo;
        private static readonly IVanningDA vanningDA = DataAccess.CreateVanningDA();
        private static readonly IVanningDetailDA vanningDetailDA = DataAccess.CreateVanningDetailDA();
        private static readonly IWaveDA waveDA = DataAccess.CreateWaveDA();

        public VanningBLL()
        {
            this.pubParasInfo = null;
        }

        public VanningBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public VanningBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteVanning(List<string> vanningID)
        {
            string VanningID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                if (vanningDA.DeleteVanning(dataBase, tran, vanningID, this.dataBaseID[0]) > 0)
                {
                    for (int i = 0; vanningID.Count > i; i++)
                    {
                        VanningID = VanningID + vanningID[i] + ";";
                    }
                    vanningDetailDA.DeleteVanningDetail(dataBase, tran, vanningID, this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return VanningID;
        }

        public string DeleteVanningContainer(string vanningID, List<string> containerID)
        {
            string VanningID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                VanningID = vanningID + "/";
                for (int i = 0; containerID.Count > i; i++)
                {
                    if (vanningDetailDA.DeleteVanningDetailByContainerID(dataBase, tran, vanningID, containerID[i], this.dataBaseID[0]) > 0)
                    {
                        VanningID = VanningID + containerID[i] + ";";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return VanningID;
        }

        public VanningInfo GetVanningByID(string vanningID)
        {
            VanningInfo vanningInfo = vanningDA.GetVanningByID(vanningID, this.dataBaseID[0], this.pubParasInfo);
            if (vanningInfo != null)
            {
                vanningInfo.VanningDetailInfos = vanningDetailDA.GetVanningDetailByOrder(vanningID, this.dataBaseID[0]);
            }
            return vanningInfo;
        }

        public List<VanningDetailInfo> GetVanningDetailByID(string vanningID, string containerID)
        {
            VanningInfo vanningInfo = vanningDA.GetVanningByID(vanningID, this.dataBaseID[0], this.pubParasInfo);
            List<VanningDetailInfo> vanningDetailList = new List<VanningDetailInfo>();
            if (vanningInfo == null)
            {
                throw new Exception("此装箱单号不存在!");
            }
            if ((vanningInfo.OrderType != null) && (vanningInfo.OrderType == "ORDER"))
            {
                if ((vanningInfo.VanningType != null) && (vanningInfo.VanningType == "ByOrderDetail"))
                {
                    vanningDetailList = vanningDetailDA.GetVanningDetailByIDOrder(vanningID, vanningInfo.OrderID, containerID, this.dataBaseID[0]);
                }
                else if ((vanningInfo.VanningType != null) && (vanningInfo.VanningType == "ByPickDetail"))
                {
                    vanningDetailList = vanningDetailDA.GetVanningDetailByIDPick(vanningID, vanningInfo.OrderID, containerID, this.dataBaseID[0]);
                }
            }
            else if ((vanningInfo.OrderType != null) && (vanningInfo.OrderType == "WAVE"))
            {
                if ((vanningInfo.VanningType != null) && (vanningInfo.VanningType == "ByOrderDetail"))
                {
                    vanningDetailList = vanningDetailDA.GetVanningDetailByIDWaveOrder(vanningID, vanningInfo.OrderID, containerID, this.dataBaseID[0]);
                }
                else if ((vanningInfo.VanningType != null) && (vanningInfo.VanningType == "ByPickDetail"))
                {
                    vanningDetailList = vanningDetailDA.GetVanningDetailByIDWavePick(vanningID, vanningInfo.OrderID, containerID, this.dataBaseID[0]);
                }
            }
            VanningDetailInfo vanningDetailInfo = vanningDetailDA.GetVanningDetailByID(vanningID, containerID, this.dataBaseID[0]);
            vanningDetailList.Add(vanningDetailInfo);
            return vanningDetailList;
        }

        public DataSet GetVanningInfoByQueryEntity(VanningQueryEntity vanningQueryEntity)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(vanningQueryEntity.VanningID))
            {
                sqlWhere.Add(" V.VANNING_ID " + vanningQueryEntity.VanningID);
            }
            if (!string.IsNullOrEmpty(vanningQueryEntity.OrderID))
            {
                sqlWhere.Add(" V.ORDER_ID " + vanningQueryEntity.OrderID);
            }
            if (!string.IsNullOrEmpty(vanningQueryEntity.OrderType))
            {
                sqlWhere.Add(" V.ORDER_TYPE " + vanningQueryEntity.OrderType);
            }
            if (!string.IsNullOrEmpty(vanningQueryEntity.Status))
            {
                sqlWhere.Add(" V.STATUS " + vanningQueryEntity.Status);
            }
            if (!string.IsNullOrEmpty(vanningQueryEntity.VanningDate))
            {
                sqlWhere.Add(vanningQueryEntity.VanningDate.Replace("@VanningDate@", "V.VANNING_DATE"));
            }
            if (!string.IsNullOrEmpty(vanningQueryEntity.Remark))
            {
                sqlWhere.Add(" V.REMARK " + vanningQueryEntity.Remark);
            }
            return vanningDA.GetVanningByQueryList(sqlWhere, vanningQueryEntity, this.dataBaseID[0], this.pubParasInfo);
        }

        public string InsertVanning(VanningInfo vanningInfo)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string VanningID = string.Empty;
            try
            {
                if (!ordersDA.CheckOrdersIDUnique(vanningInfo.OrderID, this.dataBaseID[0]) && !waveDA.CheckWaveIDUnique(vanningInfo.OrderID, this.dataBaseID[0]))
                {
                    throw new Exception("此订单不存在!");
                }
                if (!string.IsNullOrEmpty(vanningInfo.VanningID) && vanningDA.CheckVanningIDUnique(vanningInfo.VanningID, this.dataBaseID[0]))
                {
                    throw new Exception("此装箱单号已存在!");
                }
                if (string.IsNullOrEmpty(vanningInfo.VanningID))
                {
                    vanningInfo.VanningID = commonMethod.GenNextNumStr(dataBase, tran, "VANNING", this.dataBaseID[0]);
                }
                if (vanningInfo != null)
                {
                    vanningInfo.CreatedBy = this.pubParasInfo.UserID;
                    vanningInfo.UpdatedBy = this.pubParasInfo.UserID;
                    vanningInfo.CreatedDate = DateTime.Now;
                    vanningInfo.UpdatedDate = DateTime.Now;
                    if (vanningDA.CheckVanningIDUnique(dataBase, tran, vanningInfo.VanningID, this.dataBaseID[0]))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F7600_002", this.pubParasInfo.UserLanguage, new List<string> { vanningInfo.VanningID });
                    }
                    if (vanningDA.InsertVanning(dataBase, tran, vanningInfo, this.dataBaseID[0]) > 0)
                    {
                        VanningID = vanningInfo.VanningID;
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return VanningID;
        }

        public string InsertVanningDetail(List<VanningDetailInfo> vanningDetailList)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string containerID = string.Empty;
            try
            {
                foreach (VanningDetailInfo vanningDetailInfo in vanningDetailList)
                {
                    vanningDetailInfo.CreatedBy = this.pubParasInfo.UserID;
                    vanningDetailInfo.UpdatedBy = this.pubParasInfo.UserID;
                    vanningDetailInfo.CreatedDate = DateTime.Now;
                    vanningDetailInfo.UpdatedDate = DateTime.Now;
                    if (!cartonDA.CheckCartonUniqueByCartonType(dataBase, tran, vanningDetailInfo.ContainerType, this.dataBaseID[0]))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F7600_001", this.pubParasInfo.UserLanguage, new List<string> { vanningDetailInfo.ContainerType });
                    }
                    if (vanningDetailDA.CheckVanningDetailIDUnique(dataBase, tran, vanningDetailInfo.VanningID, vanningDetailInfo.ContainerID, this.dataBaseID[0]))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F7600_002", this.pubParasInfo.UserLanguage, new List<string> { vanningDetailInfo.ContainerID });
                    }
                    if (vanningDetailDA.InsertVanningDetail(dataBase, tran, vanningDetailInfo, this.dataBaseID[0]) > 0)
                    {
                        containerID = containerID + vanningDetailInfo.ContainerID + ",";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return containerID;
        }

        public string UpdateVanning(VanningInfo vanningInfo)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string VanningID = string.Empty;
            try
            {
                if (vanningDA.UpdateVanning(vanningInfo, this.dataBaseID[0]) > 0)
                {
                    VanningID = vanningInfo.VanningID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return VanningID;
        }

        public string UpdateVanningDetail(List<VanningDetailInfo> vanningDetailList)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string VanningID = string.Empty;
            AuditDataBLL auditDataBLL = new AuditDataBLL();
            try
            {
                if ((vanningDetailList != null) && (vanningDetailList.Count > 0))
                {
                    vanningDetailDA.DeleteVanningDetailByContainerID(vanningDetailList[0].VanningID, vanningDetailList[0].ContainerID, this.dataBaseID[0]);
                    foreach (VanningDetailInfo vanningDetailInfo in vanningDetailList)
                    {
                        if (vanningDetailInfo.IsActive == "N")
                        {
                            vanningDetailInfo.UpdatedBy = this.pubParasInfo.UserID;
                            vanningDetailInfo.UpdatedDate = DateTime.Now;
                            vanningDetailDA.UpdateVanningDetail(dataBase, tran, vanningDetailInfo, this.dataBaseID[0]);
                        }
                        else if (vanningDetailInfo.Qty > 0M)
                        {
                            vanningDetailInfo.UpdatedBy = this.pubParasInfo.UserID;
                            if (vanningDetailDA.InsertVanningDetail(dataBase, tran, vanningDetailInfo, this.dataBaseID[0]) > 0)
                            {
                                VanningID = vanningDetailInfo.ContainerID;
                            }
                        }
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return VanningID;
        }
    }
}

